package cn.itsource.hrm.exception;

import org.springframework.util.StringUtils;
import sun.applet.Main;

import java.util.List;

public class ValidUtils {
    //断言不为空
    public static void assertNotNull(Object obj,String message){
        if(null == obj){
            throw new GlobalException(message);
        }
        if(obj instanceof String){
            String objStr = (String) obj;
            if(objStr.length() == 0){
                throw new GlobalException(message);
            }
        }
    }

    /**
     * 期望为null
     * @param list
     * @param message
     */
    public static void assertListNull(List list, String message){
        if(list != null && list.size()>0){
            throw new GlobalException(message);
        }
    }
    /**
     * 期望不为null
     * @param list
     * @param message
     */
    public static void assertListNotNull(List list, String message){
        if(list == null || list.size()<1){
            throw new GlobalException(message);
        }
    }
    public static void assertBoolean(boolean v,String message){
        if(!v){
            throw new GlobalException(message);
        }
    }
    public static void assertIntEqual(int v1,int v2,String message){
        if(v1==v2){
            throw new GlobalException(message);
        }
    }

    /**
     * 不区分大小写比对
     * @param str1
     * @param str2
     * @param message
     */
    public static void assertEqualIgnoreCase(String str1,String str2,String message){
        if(StringUtils.isEmpty(str1)|| StringUtils.isEmpty(str2)){
            throw new GlobalException(message);
        }
        if (!str1.trim().equalsIgnoreCase(str2.trim()))
            throw new GlobalException(message);
    }

    public static void main(String[] args) {
        System.out.println("ABc".equals("abc"));
        System.out.println("ABc".equalsIgnoreCase("abc"));
    }

    /**
     * 区分大小写比对
     * @param str1
     * @param str2
     * @param message
     */
    public static void assertEqual(String str1,String str2,String message){
        if(StringUtils.isEmpty(str1)|| StringUtils.isEmpty(str2)){
            throw new GlobalException(message);
        }
        if (!str1.trim().equals(str2.trim()))
            throw new GlobalException(message);
    }


    //========以下两个方法支持错误吗=========/
    public static void assertEqual(String str1,String str2,int code,String message){
        if(StringUtils.isEmpty(str1)|| StringUtils.isEmpty(str2)){
            throw new GlobalException(code,message);
        }
        if (!str1.trim().equals(str2.trim()))
            throw new GlobalException(code,message);
    }

    public static void assertEqual(String str1,String str2,ErrorCode code){
        if(StringUtils.isEmpty(str1)|| StringUtils.isEmpty(str2)){
            throw new GlobalException(code.getCode(),code.getMessage());
        }
        if (!str1.trim().equals(str2.trim()))
            throw new GlobalException(code.getCode(),code.getMessage());
    }
}
